package org.ahuanlien.crm.util;


import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 异常控制
 */
@ControllerAdvice
public class HandlerExceptionUtil {


	@ExceptionHandler(UnauthorizedException.class)
	public void handlerUnauthorizedException(HttpServletResponse response, HandlerMethod handlerMethod) throws IOException {
		//获取请求是是返回json还是视图
		ResponseBody responseBody = handlerMethod.getMethodAnnotation(ResponseBody.class);
		if (responseBody != null) {
			String json = JSON.toJSONString(JSONResult.mark("没有权限访问"));
			response.getWriter().print(json);
		} else {
			response.sendRedirect("/nopermission.jsp");
		}
	}

}
